package tt.dz.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsMerchantList;
import tt.dz.entity.system.OmsOperList;
import tt.dz.service.base.MerchantListService;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant;

/**
 * 商户的controller
 * @author huanghao
 *
 */
@Controller
@RequestMapping("/merchantListApi")
public class MerchantListAction extends BaseController
{
	@Autowired
	private MerchantListService merchantListServiceImpl;
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
	{
		return merchantListServiceImpl.add(this.getObjFromRequest(request,true,false));
	}
	
	@RequestMapping(value="update/{merchant_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateOperMerchant(@PathVariable(value="merchant_id") String merchant_id,
			HttpServletRequest request, HttpServletResponse response)
	{
		OmsMerchantList merchant = this.getObjFromRequest(request,false,false);
		merchant.setMerchant_id(merchant_id);
		return merchantListServiceImpl.update(merchant);
	}
	
	@RequestMapping(value="delete/{merchant_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(@PathVariable(value="merchant_id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return merchantListServiceImpl.delete(id);
	}
	
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request)
	{
		OmsMerchantList queryObj = this.getObjFromRequest(request,false,true);
		HttpSession session = request.getSession();
		OmsOperList user =  (OmsOperList) session.getAttribute(BaseAction.CommonParam.SESSION_USER);
		if (user.getIsSupper() < 0)
		{
			queryObj.setEntity_id(user.getEntity_id());
			queryObj.setEntity_type(user.getEntity_type());
		}
		return merchantListServiceImpl.findMerchantLstPage(ActionUtils.buildQueryParam(request, true), this.getObjFromRequest(request, false, true));
	}

	@RequestMapping(value = "findObj/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findObj(@PathVariable(value = "id") String id)
	{
		return merchantListServiceImpl.findMerchantById(id);
	}

	@RequestMapping(value = "syncFromMS")
	@ResponseBody
	public JsonCommon<?> syncFromMS() throws Exception
	{
		merchantListServiceImpl.syncPartnerFromMasterServer();
		return new JsonCommon(null, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
	
	private OmsMerchantList getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		HttpSession session = request.getSession();
		String loginUserId =  (String) session.getAttribute(BaseAction.CommonParam.SESSION_USERID);
		return ActionUtils.getParamInstance(OmsMerchantList.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}
